package cn.cleanarch.infra.idp.web.config;

import cn.cleanarch.infra.idp.commons.utils.IdGenerator;
import cn.cleanarch.infra.idp.web.client.I18nDatabaseSource;
import cn.cleanarch.infra.idp.web.utils.I18nMessageTool;
import cn.cleanarch.infra.idp.web.utils.SpringBeanTool;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BaseMiddlewareConfiguration implements InitializingBean {

    @Value("${project.machine-id:1}")
    private Integer machineId;
    @Value("${project.datacenter-id:1}")
    private Integer datacenterId;

    private final ApplicationContext applicationContext;

    public BaseMiddlewareConfiguration(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 更改雪花算法的machineId、datacenterId避免分布式环境下产生相同的id
        IdGenerator.register(machineId.longValue(), datacenterId.longValue());
        // 某些场景，需要通过静态方法获取到Bean，所以要用一个静态变量，指向 applicationContext
        SpringBeanTool.setApplicationContext(applicationContext);

        // 使得 I18nMessageTool 支持通过读取数据库的方式获取翻译
        try {
            I18nDatabaseSource messageSource = applicationContext.getBean(
                    I18nDatabaseSource.class);
            I18nMessageTool.setDatabaseSource(messageSource);
        } catch (BeansException e) {
            // 可选，允许不设置DatabaseI18nMessageSource
        }
    }
}
